package team.scau.laboratory_repair.web.sys;

import lombok.extern.apachecommons.CommonsLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import team.scau.laboratory_repair.common.base.BaseController;
import team.scau.laboratory_repair.service.FileService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;

/**
 *
 * @author 30254
 * @date 2018/8/25
 */
@Controller
@CommonsLog
public class UploadController extends BaseController {
    @Resource
    private FileService fileService;

    @RequestMapping(value = "/upload", produces = "text/html;charset=utf-8")
    public String upload() throws Exception {
        return "upload";
    }

    private Map upload(HttpServletRequest request, String savePath, Predicate<String> judgeFile) throws Exception{
        String result = null;
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        String fieldName = multipartRequest.getParameter("fieldName");
        MultipartFile multipartFile = multipartRequest.getFile(fieldName);// 获取上次文件名
        if (StringUtils.isNotEmpty(multipartFile.getOriginalFilename())) {
            //获取到文件保存的根目录，也就是webapp/upload目录
            String uploadfile = this.getDocRoot(request)+ savePath;
            if (!judgeFile.test(multipartFile.getOriginalFilename())) {
                result = "error";
            }else{
                //处理上传
                File file = fileService.saveFile(multipartFile, uploadfile);
                if (file != null) {
                    result = file.getName();
                } else {
                    result = "error";
                }
            }

        } else {
            result = "error";
        }

        Map map=new HashMap<>();
        map.put("filePath",result);
        return map;
    }


    /**
     * 处理图片的上传
     * @param request
     * @throws Exception
     */
    //@RequiresAuthentication
    @PostMapping(value = "/img/upload", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map uploadImg(HttpServletRequest request) throws Exception {
        return upload(request, "/ueditor/image", (fileName) -> {
            if(!FileService.isImg(fileName)){
                log.error("您上传的不是图片！");
                return false;
            }
            return true;
        });
    }

    /**
     * 处理文件的上传
     * @param request
     * @throws Exception
     */
    //@RequiresAuthentication
    @PostMapping(value = "/file/upload", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map uploadFile(HttpServletRequest request) throws Exception {
        return upload(request, "/ueditor/file", (fileName) -> true);
    }

    /**
     * 处理文件的上传
     * @param request
     * @throws Exception
     */
    //@RequiresAuthentication
    @PostMapping(value = "/video/upload", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map uploadVideo(HttpServletRequest request) throws Exception {
        return upload(request, "/ueditor/video", (fileName) -> true);
    }

}
